package bancosys.tec.security.impl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jmine.tec.persist.schema.impl.elements.SequenceElement;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.security.impl.domain.Credential;

/**
 * @author seiti
 * @created Apr 27, 2011
 */
public class CredentialDAO extends AbstractCredentialDAO<Credential> {

    /**
     * Dada uma lista de nomes de grupos devolve as todas as credenciais associadas à estes grupos, se eles existirem no BD.
     * 
     * @param groupNames lista de nomes de grupos
     * @return lista de credenciais associadas aos grupos
     */
    @SuppressWarnings("deprecation")
    public List<Credential> findByGroupNames(List<String> groupNames) {
        StringBuilder queryString = new StringBuilder();
        queryString.append(" SELECT DISTINCT credentials ");
        queryString.append(" FROM Group as g ");
        queryString.append(" INNER JOIN g.credentials as credentials ");
        queryString.append(" WHERE g.name IN  ( :groupNames )");

        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("groupNames", groupNames);

        return this.executeQuery(queryString.toString(), params);
    }

    /**
     * Busca todas as credenciais em postscript.
     * 
     * @return List de Credential
     */
    public List<Credential> findAllPostscriptCredentials() {
        Criteria criteria = this.createCriteria();
        criteria.add(Restrictions.lt("id", SequenceElement.DEFAULT_INITIAL_VALUE));
        criteria.addOrder(Order.asc("id"));
        return this.executeQuery(criteria);
    }
}
